﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DoAnWebsite.Models;
using Ext.Net;
using Ext.Net.MVC;
using System.Linq.Dynamic;

namespace DoAnWebsite.Controllers
{
    public class CancerController : Controller
    {
        //Cancer
        private static DoAnWebsite_Entities db = new DoAnWebsite_Entities();
        [AccessDeniedAuthorize(Roles = "administrators, users")]
        public ActionResult ListCancer()
        {
            Session["includeInActiveCancer"] = false;
            Session["CharFilterCancer"] = "";
            return View();
        }

        public ActionResult IncludeInActive()
        {
            var store = X.GetCmp<Store>("CancerStore");

            Session["includeInActiveCancer"] = !(bool)Session["includeInActiveCancer"];

            store.Reload();
            return this.Direct();
        }

        [DirectMethod]
        public ActionResult CharFilterCancer(string filterstring)
        {
            var store = X.GetCmp<Store>("CancerStore");
            Session["CharFilterCancer"] = filterstring;
            store.Reload();
            return this.Direct();
        }

        public ActionResult BindingCancerStore(StoreRequestParameters parameters)
        {
            var sampleData = (db.Cancers.Select(m=> new
                                                        {
                                                            m.CancerId,
                                                            m.CancerName,
                                                            m.Active
                                                        })
                             );


            bool includeInActive = (bool)Session["includeInActiveCancer"];
            string charFilter = (string)Session["CharFilterCancer"];

            var result = sampleData.Where(ent => ((includeInActive || (bool)ent.Active)));
            if ("0".Equals(charFilter))
            {
                result = sampleData.Where(ent => ((includeInActive || (bool)ent.Active) &&
                    (ent.CancerName.StartsWith("0") || ent.CancerName.StartsWith("1") ||
                    ent.CancerName.StartsWith("2") || ent.CancerName.StartsWith("3") ||
                    ent.CancerName.StartsWith("4") || ent.CancerName.StartsWith("5") ||
                    ent.CancerName.StartsWith("6") || ent.CancerName.StartsWith("7") ||
                    ent.CancerName.StartsWith("8") || ent.CancerName.StartsWith("9")
                    )));
            }

            else
            {
                result = sampleData.Where(ent => ((includeInActive || (bool)ent.Active) && ent.CancerName.StartsWith(charFilter)));

            }

            // Sorting
            if (!string.IsNullOrEmpty(parameters.SimpleSort))
            {
                result = result.OrderBy(parameters.SimpleSort + (parameters.SimpleSortDirection == SortDirection.DESC ? " desc" : " asc"));
            }
            else
            {
                result = result.OrderBy(ent => ent.CancerId);
            }

            // Paging
            int limit = parameters.Limit;
            if ((parameters.Start + parameters.Limit) > result.Count())
            {
                limit = result.Count() - parameters.Start;
            }

            var rangeData = (parameters.Start < 0 || limit < 0) ? result : result.Skip(parameters.Start).Take(limit);
            return this.Store(rangeData.Select(ent => new { ent.CancerId, ent.CancerName, ent.Active }), result.Count());
        }

        [HttpGet]
        [AccessDeniedAuthorize(Roles = "administrators, users")]
        public ActionResult CreateCancer()
        {
            ViewBag.HideTextBox = false;
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult CreateCancer(Cancer model)
        {
            if (ModelState.IsValid)
            {
                model.Active = true;
                db.Cancers.AddObject(model);
                db.SaveChanges();
                X.Msg.Notify("Sucess", "Create Cancer success!").Show();
                return this.FormPanel();
            }

            X.Msg.Notify("Non Sucess", "Create Cancer non success!").Show();

            return this.FormPanel();
        }

        [HttpGet]
        [AccessDeniedAuthorize(Roles = "administrators, users")]
        public ActionResult EditCancer(int id)
        {
            ViewBag.HideTextBox = false;
            Cancer cancer = db.Cancers.Single(m => m.CancerId == id);
            return View(cancer);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditCancer(Cancer model)
        {
            Cancer cancer1 = db.Cancers.Single(c => c.CancerId == model.CancerId);
            if (ModelState.IsValid)
            {
                db.Cancers.Detach(cancer1);
                db.Cancers.Attach(model);
                db.ObjectStateManager.ChangeObjectState(model, EntityState.Modified);
                db.SaveChanges();
                X.Msg.Notify("Sucess", "Edit Cancer success!").Show();
                return this.FormPanel();
            }
            X.Msg.Notify("Non Sucess", "Edit Cancer non success!").Show();
            return this.FormPanel();
        }

    }
}
